package com.bai.localshop.controller.admin;

import com.bai.localshop.pojo.dto.UserPageQueryDTO;
import com.bai.localshop.pojo.dto.UserSaveDTO;
import com.bai.localshop.pojo.dto.UserUpdateDTO;
import com.bai.localshop.pojo.vo.UserPageVO;
import com.bai.localshop.pojo.vo.UserDetailsVO;
import com.bai.localshop.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bai.localshop.result.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理控制器
 */
@RestController
@RequestMapping("/api/admin/users")
@RequiredArgsConstructor
@Slf4j
public class UserController {

    private final UserService userService;

    /**
     * 分页查询用户
     */
    @GetMapping("/page")
    public Result<IPage<UserPageVO>> pageQuery(UserPageQueryDTO requestParam) {
        return Result.success(userService.pageQuery(requestParam));
    }

    /**
     * 根据id查询用户
     */
    @GetMapping("/{id}")
    public Result<UserDetailsVO> selectGetById(@PathVariable("id") Long id) {
        return Result.success(userService.selectGetById(id));
    }

    /**
     * 新增用户
     */
    @PostMapping
    public Result<String> save(@RequestBody UserSaveDTO userSaveDTO) {
        userService.save(userSaveDTO);
        return Result.success("用户新增成功");

    }

    /**
     * 修改用户
     */
    @PutMapping("/update")
    public Result<String> updateGetById(@RequestBody UserUpdateDTO requestParam){
        userService.updateGetById(requestParam);
        return Result.success("用户修改成功");
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteById(@PathVariable Long id){
        userService.deleteById(id);
        return Result.success("用户删除成功");
    }

    /**
     * 更新用户状态
     */
    @PutMapping("/{id}/status")
    public Result<String> updateStatus(@PathVariable Long id, @RequestParam Integer status){
        userService.updateStatus(id,status);
        return Result.success("状态更新成功");
    }

    /**
     * 重置用户密码
     */
    @PutMapping("/{id}/password")
    public Result<String> resetPassword(@PathVariable Long id) {
        userService.resetPassword(id);
        return Result.success("密码重置成功");
    }
}